package com.jackchong.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.jackchong.base.BaseToolsActivity;
import com.jackchong.utils.DeviceInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by chong on 2017/7/28.
 */

public class MainEventLayout extends JFrameLayout {

    private List<OnDispatchTouchEventListener> mOnDispatchTouchEventListeners = new ArrayList<>();
    private OnTouchListener mOnTouchListener;

    private OnDownListener mOnDownListener;
    private OnActivityFinish mOnActivityFinish;
    private boolean mSlideCloseEnable = true;

    private float mOldMoveX;
    private float mOldMoveY;

    private float mTotalMove;

    private long mLastTime;

    private BaseToolsActivity mActivity;

    private boolean isClose;
    private int mScreenWide;

    public MainEventLayout(Context context) {
        super(context);
        mScreenWide = DeviceInfo.sScreenRect.width();
    }

    public MainEventLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        mScreenWide = DeviceInfo.sScreenRect.width();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return down(ev);
            case MotionEvent.ACTION_MOVE:
                move(ev);
                break;
            case MotionEvent.ACTION_UP:
                return up(ev);
            case MotionEvent.ACTION_CANCEL:
                cancel(ev);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean down(MotionEvent ev) {
        mTotalMove = 0;
        mOldMoveX = ev.getX();
        mOldMoveY = ev.getY();
        if (mOnDownListener != null) mOnDownListener.onDown(ev);

        for (OnDispatchTouchEventListener onDispatchTouchEventListener : mOnDispatchTouchEventListeners) {
            if (onDispatchTouchEventListener.onDown(ev)){
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private void move(MotionEvent ev) {

        for (OnDispatchTouchEventListener onDispatchTouchEventListener : mOnDispatchTouchEventListeners) {
            onDispatchTouchEventListener.onMove(ev);
        }
        if (mSlideCloseEnable){
            if (mOldMoveX == 0) mOldMoveX = ev.getX();
            if (mOldMoveY == 0) mOldMoveY = ev.getY();
            if (mLastTime == 0) mLastTime = System.currentTimeMillis();
            if (ev.getX() - mOldMoveX < 0) mTotalMove += Math.abs(ev.getX() - mOldMoveX);
            if (ev.getX() - mOldMoveX > 14 * mScreenWide / 1080 && ev.getX() - mOldMoveX >
                    Math.abs(ev.getY() - mOldMoveY) * 2f && mTotalMove < 40 * mScreenWide / 1080) {
                isClose = true;
                mLastTime = System.currentTimeMillis();
            }else {
                isClose = false;
            }
            mOldMoveX = ev.getX();
            mOldMoveY = ev.getY();
        }
    }

    private boolean up(MotionEvent ev) {
        if (mSlideCloseEnable){
            if (isClose && System.currentTimeMillis() - mLastTime < 200 && mActivity != null) {
                if (mOnActivityFinish == null || mOnActivityFinish.onFinish()){
                    mActivity.finishActivity();
                    return true;
                }
            }
        }
        for (OnDispatchTouchEventListener onDispatchTouchEventListener : mOnDispatchTouchEventListeners) {
            onDispatchTouchEventListener.onUp(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

    private void cancel(MotionEvent ev) {
        for (OnDispatchTouchEventListener onDispatchTouchEventListener : mOnDispatchTouchEventListeners) {
            onDispatchTouchEventListener.onCancel(ev);
        }
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mOnTouchListener != null) mOnTouchListener.onTouch(event);
        return true;
    }

    public void setSlideCloseEnable(boolean slideCloseEnable, BaseToolsActivity activity) {
        mSlideCloseEnable = slideCloseEnable;
        mActivity = activity;
    }

    public void setSlideCloseEnable(boolean slideCloseEnable) {
        mSlideCloseEnable = slideCloseEnable;
    }


    public abstract static class OnDispatchTouchEventListener{
        public boolean onDown(MotionEvent event){ return false;}
        public void onMove(MotionEvent event){}
        public void onUp(MotionEvent event){}
        public void onCancel(MotionEvent event){}
    }

    /**
     * 左滑关闭Activity的监听
     */
    public interface OnActivityFinish{

        /**
         * 在左滑手势满足条件时回调,如果不设置改监听,会默认关闭Activity
         * @return true则会关闭Activity,false 则不会
         */
        boolean onFinish();
    }

    public interface OnDownListener{
        void onDown(MotionEvent event);
    }

    public interface OnTouchListener{
        void onTouch(MotionEvent event);
    }

    public void addOnDispatchTouchEventListener(OnDispatchTouchEventListener onDispatchTouchEventListener){
        mOnDispatchTouchEventListeners.add(onDispatchTouchEventListener);
    }

    public void setOnActivityFinish(OnActivityFinish onActivityFinish) {
        this.mOnActivityFinish = onActivityFinish;
    }

    public void setOnDownListener(OnDownListener onDownListener){
        mOnDownListener = onDownListener;
    }

    public void setOnTouchEvent(OnTouchListener onTouchEvent){
        mOnTouchListener = onTouchEvent;
    }
}
